package com.relax.game.casino.forward;

import java.util.Random;

public class CardBox {
	public static int CO = 3;
	public static int RO = 2;
	public static int CHUONG = 1;
	public static int BICH = 0;
	
	private Card[] cards = null;
	
	public CardBox() {
		initCard();		
	}
	
	private void initCard() {
		cards = new Card[52];
		
		int num = 0;
		for (int i = 0; i < 4; i++) {
			for (int j = 3; j < 16; j++) {
				Card card = new Card(i, j);
				cards[num] = card;
				num++;
			}
		}	
	}
	
	public void mixCard() {
		Random random = new Random();
		int numLoop = generateRandomInteger(50, 200, random);
		for (int i = 0; i < numLoop; i++) {
			this.mixCardOneTime();
		}
	}
	
	private void mixCardOneTime() {
		Random random = new Random();
		int startIndex = generateRandomInteger(0, 51, random);
		int endIndex = startIndex;
		
		while (endIndex == startIndex) {
			endIndex = generateRandomInteger(0, 51, random);
		}
		
		if (endIndex < startIndex) {
			int temp = endIndex;
			endIndex = startIndex;
			startIndex = temp;
		}
		
		Card[] cardTemp = new Card[endIndex - startIndex + 1];
		for (int i = startIndex; i < endIndex + 1; i++) {
			cardTemp[i - startIndex] = this.cards[i];
		}
		
		for (int i = startIndex - 1; i > 0; i--) {
			this.cards[endIndex - (startIndex - 1 - i)] = this.cards[i];
		}
		
		for (int i = 0; i < cardTemp.length; i++) {
			this.cards[i] = cardTemp[i];
		}
	}
	
	private int generateRandomInteger(int aStart, int aEnd, Random aRandom){
	    if ( aStart > aEnd ) {
	      throw new IllegalArgumentException("Start cannot exceed End.");
	    }
	    //get the range, casting to long to avoid overflow problems
	    long range = (long)aEnd - (long)aStart + 1;
	    // compute a fraction of the range, 0 <= frac < range
	    long fraction = (long)(range * aRandom.nextDouble());
	    int randomNumber =  (int)(fraction + aStart);
	    return randomNumber;
	 }
}
